<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="/js/include.js" type="text/javascript" charset="utf-8"></script>
    <style>
        .layui-upload-img {
            width: 30%;
            height: 30%;
            margin: 0 10px 10px 0;
        }
    </style>
</head>
<body>

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>资源详情</legend>
</fieldset>

<div id="fromtest"></div>

<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>

    var url = window.location.search;
    var stringbuffer='<form class="layui-form" id="from1" action="">';
    var datalist=JSON.parse(decodeURI(parseJson(url).datalist))[0];
    //console.log(datalist)

    datalist.map(function (item) {
        //过滤掉系统字段
        if(item.is_system=="false"){
            stringbuffer +=
                    '<div class="layui-form-item">'+
                    '<label class="layui-form-label">' + item.title + '</label>'+
                    '<div class="layui-input-block">'+
                    '<input type="text" name="' + item.field + '"  placeholder="' + item.title + '" class="layui-input">'+
                    '</div>'+
                    '</div>';
        }
    })
    stringbuffer+='<div class="layui-form-item">'+
                        '<div class="layui-input-block">'+
                            '<button class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>'+
                        '</div>'+
                    '</div>';
    stringbuffer+='</form>';
    var div1=$(stringbuffer);


    $("#fromtest").html(div1);
    var table_name = parseJson(url).resource_name;

    layui.use(['form', 'layedit'], function () {
        var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit,
                upload = layui.upload;
        init();

        form.on('submit(demo1)', function (data) {
            console.log(data.field)
            $.ajax({
                url: "/data/table/save",
                type: "post",
                data: {submit_data: JSON.stringify(data.field), resource_name: table_name},
                success: function (result) {
                    if (result.success) {
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index);
                        parent.query();
                    } else {
                        layer.msg(result.message)
                    }
                }
            })
            return false;
        });

    });

    $(".cancle").click(function () {
        window.parent.layer.closeAll();
    })

    function parseJson(param) {
        var obj = {};
        var keyvalue = [];
        var key = "", value = "";
        var paraString = param.split("&");
        for (var i in paraString) {
            keyvalue = paraString[i].split("=");
            key = keyvalue[0];
            value = keyvalue[1];
            obj[key] = value;
        }
        return obj;
    }

    function init() {

        if (url != null && url != "") {
            url = url.substr(1, url.length);
            if (parseJson(url).id != "") {
                $.ajax({
                    url: "/data/table/detail",
                    data: {id: parseJson(url).id, resource_name: table_name},
                    async: false,
                    success: function (data) {
                        if (data.success) {
                            $.each(data.data, function (name, value) {
                                console.log(name)
                                var t = $("#from1" + ' *[name=' + name + ']');
                                if (t.prop("localName") == "textarea") {
                                    $("textarea[name='" + name + "']").val(value)
                                }
                                if (t.prop("localName") == "input") {
                                    $("input[name='" + name + "']").val(value)
                                }
                                if (name == "logo_image") {
                                    $("#logo_image").prop("src", value);
                                }
                                if (t.prop("localName") == "select") {
                                    $("select[name='" + name + "']").val(value)
                                }
                            })
                        } else {
                            layer.msg(data.message)
                        }
                    }
                })
            }
        }
    }
</script>

</body>
</html>